﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
using System.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using com.evaair.mall;

public partial class OrderDetailAndPayment : System.Web.UI.Page
{
    OrderInfo oi;
    protected void Page_Load(object sender, EventArgs e)
    {
        //購物車為null或是無商品導回首頁
        if (Session["ShoppingCart"] == null || ((ShoppingCart)Session["ShoppingCart"]).GetItemCount() == 0)
        {
            Response.Redirect("~/Home.aspx");
        }

        oi = (OrderInfo)Session["OrderInfo"];
        if (!Page.IsPostBack)
        {
            txtBuyerZIP.Attributes.Add("ReadOnly", "ReadOnly");
            txtReceiverZIP.Attributes.Add("ReadOnly", "ReadOnly");
            txtCharityCode.Attributes.Add("ReadOnly", "ReadOnly");
            //取得購物清單資料列表與計算費用
            BindShoppingList();
            //設定生日與信用卡有效期限選項
            SetYearMonth();
            //取得訂單資訊
            GetOrderInfo();
            //設定付款方式至Hidden欄位
            hfPayType.Value = oi.PayType; //付款方式
            hfDiscType.Value = oi.DiscType; //折扣方式
        }
    }

    /// <summary>
    /// 取得訂單資訊
    /// </summary>
    protected void GetOrderInfo()
    {
        txtBuyerName.Text = string.IsNullOrEmpty(oi.BuyerName) ? string.Empty : oi.BuyerName;
        txtBuyerID.Text = string.IsNullOrEmpty(oi.BuyerID) ? string.Empty : oi.BuyerID;
        //非會員以身份證登入不可修改該欄
        if (oi.BuyerID.Length > 0 & oi.BuyerID == oi.MemberID)
        {
            txtBuyerID.Attributes.Add("ReadOnly", "ReadOnly");
        }
        else
        {
            txtBuyerID.Attributes.Remove("ReadOnly");
        }

        if (!string.IsNullOrEmpty(oi.BuyerSex))
        {
            ddlBuyerSex.SelectedValue = oi.BuyerSex;
        }
        if (!string.IsNullOrEmpty(oi.BuyerBirthday))
        {
            ddlYear.SelectedValue = oi.BuyerBirthday.Substring(0, 4);
            ddlMonth.SelectedValue = oi.BuyerBirthday.Substring(4, 2);
            hiddenDay.Value = oi.BuyerBirthday.Substring(6, 2);
        }
        txtBuyerPhone.Text = string.IsNullOrEmpty(oi.BuyerPhone) ? string.Empty : oi.BuyerPhone;
        txtBuyerFax.Text = string.IsNullOrEmpty(oi.BuyerFax) ? string.Empty : oi.BuyerFax;
        txtBuyerTel.Text = string.IsNullOrEmpty(oi.BuyerTel) ? string.Empty : oi.BuyerTel;
        txtBuyerMobile.Text = string.IsNullOrEmpty(oi.BuyerMobile) ? string.Empty : oi.BuyerMobile;
        txtBuyerEamil.Text = string.IsNullOrEmpty(oi.BuyerEamil) ? string.Empty : oi.BuyerEamil;
        if (!string.IsNullOrEmpty(oi.EDM))
        {
            ddlEDM.SelectedValue = oi.EDM;
        }
        hfBuyerZone1.Value = string.IsNullOrEmpty(oi.BuyerCity) ? string.Empty : oi.BuyerCity;
        hfBuyerZone2.Value = string.IsNullOrEmpty(oi.BuyerArea) ? string.Empty : oi.BuyerArea;
        txtBuyerZIP.Text = string.IsNullOrEmpty(oi.BuyerZIP) ? string.Empty : oi.BuyerZIP;
        txtBuyerAddress.Text = string.IsNullOrEmpty(oi.BuyerAddress) ? string.Empty : oi.BuyerAddress;
        //發票類別
        if (oi.PayType == "1")
        {
            divCreditCard.Visible = true;
            if (!string.IsNullOrEmpty(oi.CreditType))
            {
                ddlCreditType.SelectedValue = oi.CreditType;
            }
            if (!string.IsNullOrEmpty(oi.CreditNumber))
            {
                txtCreditNumber1.Text = oi.CreditNumber.Substring(0, 4);
                txtCreditNumber2.Text = oi.CreditNumber.Substring(4, 4);
                txtCreditNumber3.Text = oi.CreditNumber.Substring(8, 4);
                txtCreditNumber4.Text = oi.CreditNumber.Substring(12);

            }
            txtAuthCode.Attributes.Add("value", string.IsNullOrEmpty(oi.AuthCode) ? string.Empty : oi.AuthCode);
            if (!string.IsNullOrEmpty(oi.CreditExpire))
            {
                ddlCreditExpireYear.SelectedValue = oi.CreditExpire.Substring(0, 4);
                ddlCreditExpireMonth.SelectedValue = oi.CreditExpire.Substring(4, 2);
            }
        }
        else
        {
            divCreditCard.Visible = false;
        }

        //發票類別
        if (!string.IsNullOrEmpty(oi.ReceiptType))
        {
            rbReceiptType1.Checked = oi.ReceiptType == "個人";
            rbReceiptType2.Checked = oi.ReceiptType == "公司";
            rbReceiptType3.Checked = oi.ReceiptType == "捐贈";
        }
        //捐贈機構
        txtCharityCode.Text = string.IsNullOrEmpty(oi.CharityCode) ? string.Empty : oi.CharityCode;
        txtBuyerCompanyID.Text = string.IsNullOrEmpty(oi.BuyerCompanyID) ? string.Empty : oi.BuyerCompanyID;
        txtBuyerCompanyTitle.Text = string.IsNullOrEmpty(oi.BuyerCompanyTitle) ? string.Empty : oi.BuyerCompanyTitle;
        //我要索取電子發票證明聯
        if (!string.IsNullOrEmpty(oi.InvoicePrint))
        {
            rbReceipt1.Checked = (oi.InvoicePrint == "Y" & oi.ReceiptType == "個人");
            cbReceiptPrint.Checked = (oi.InvoicePrint == "Y" & oi.ReceiptType == "公司");
            //列印購物明細
            cbReceiptPrintDetail.Enabled = (rbReceipt1.Checked || cbReceiptPrint.Checked);
            cbReceiptPrintDetail.Checked = (!string.IsNullOrEmpty(oi.InvoicePrintDetail) && oi.InvoicePrintDetail == "Y");
        }

        //手機條碼 
        if (!string.IsNullOrEmpty(oi.CellPhoneCode))
        {
            rbReceipt2.Checked = true;
            txtCellPhoneCode.Enabled = true;
            txtCellPhoneCode.Text = oi.CellPhoneCode;
            txtCellPhoneCodeCheck.Text = oi.CellPhoneCode;

        }
        //自然人憑證 
        if (!string.IsNullOrEmpty(oi.CitiDigiCertificate))
        {
            rbReceipt3.Checked = true;
            txtCitiDigiCertificate.Enabled = true;
            txtCitiDigiCertificateCheck.Enabled = true;
            txtCitiDigiCertificate.Text = oi.CitiDigiCertificate;
            txtCitiDigiCertificateCheck.Text = oi.CitiDigiCertificate;
        }
        //收件人資訊
        cbCopyBuyer.Checked = oi.CopyBuyer == "Y";
        txtReceiverName.Text = string.IsNullOrEmpty(oi.ReceiverName) ? string.Empty : oi.ReceiverName;
        if (!string.IsNullOrEmpty(oi.ReceiverSex))
        {
            ddlReceiverSex.SelectedValue = oi.ReceiverSex;
        }
        txtReceiverPhone.Text = string.IsNullOrEmpty(oi.ReceiverPhone) ? string.Empty : oi.ReceiverPhone;
        txtReceiverTel.Text = string.IsNullOrEmpty(oi.ReceiverTel) ? string.Empty : oi.ReceiverTel;
        txtReceiverMobile.Text = string.IsNullOrEmpty(oi.ReceiverMobile) ? string.Empty : oi.ReceiverMobile;
        hfReceiverZone1.Value = string.IsNullOrEmpty(oi.ReceiverCity) ? string.Empty : oi.ReceiverCity;
        hfReceiverZone2.Value = string.IsNullOrEmpty(oi.ReceiverArea) ? string.Empty : oi.ReceiverArea;
        txtReceiverZIP.Text = string.IsNullOrEmpty(oi.ReceiverZIP) ? string.Empty : oi.ReceiverZIP;
        txtReceiverAddress.Text = string.IsNullOrEmpty(oi.ReceiverAddress) ? string.Empty : oi.ReceiverAddress;
        //同意電子商務網站個資蒐集
        cbAgree.Checked = (!string.IsNullOrEmpty(oi.Agree) && oi.Agree == "Y");
    }

    /// <summary>
    /// 設定生日與信用卡有效期限選項
    /// </summary>
    protected void SetYearMonth()
    {
        DateTime dt = DateTime.Now;
        // Year
        for (int i = 1914; i <= dt.Year; i++)
        {
            this.ddlYear.Items.Insert(0, new ListItem(i.ToString(), i.ToString()));
        }
        //  Month
        for (int i = 1; i <= 12; i++)
        {
            this.ddlMonth.Items.Add(new ListItem(i.ToString("00"), i.ToString("00")));
        }

        // Year
        for (int i = 0; i <= 10; i++)
        {
            this.ddlCreditExpireYear.Items.Add(new ListItem((dt.Year + i).ToString(), (dt.Year + i).ToString()));
        }
    }

    /// <summary>
    /// 取得購物清單資料列表與計算費用
    /// </summary>
    protected void BindShoppingList()
    {

        ShoppingCart userShoppingCart = (ShoppingCart)Session["ShoppingCart"];


        B2C_MALL_ORDER ws = new B2C_MALL_ORDER();
        ws.Url = ConfigurationManager.AppSettings["B2C_MALL_ORDER"].ToString();
        //購物車資料
        string strOrderList = CommonUtil.ShoppingCartToOrderListSerialize(userShoppingCart);
        //加贈資料
        List<FreePlusList> liFreePlus = (List<FreePlusList>)Session["FreePlusList"];
        string strFreePlus = CommonUtil.FreePlusListToFreePlusItemSerialize(liFreePlus);
        //加購資料(無資料傳"")
        List<BuyPlusItem> liBuyPlus = (List<BuyPlusItem>)Session["BuyPlusItem"];
        string strBuyPlus = liBuyPlus.Count > 0 ? JsonConvert.SerializeObject(liBuyPlus) : "";

        string strJson =
          ws.B2C_MALL_ValidateOrderInfo(CommonUtil.GetWebDevice(), oi.MemberID, oi.MemberType,
          oi.DiscType, oi.PayType, strOrderList.ToString(),
          "", "", "", "", oi.IsLogin, oi.Password, "2", oi.ChooseECouponNo,
          strBuyPlus, strFreePlus, oi.MileMemberID);

        JObject jObj = JsonConvert.DeserializeObject<JObject>(strJson);
        if (jObj["Is_Valid"].ToString() == "Y")
        {
            //清單資料列表
            List<ItemList> li = new List<ItemList>();
            int intNo = 1;
            //購物車
            foreach (CartItem ci in userShoppingCart.CartItems)
            {
                ItemList il = new ItemList();
                il.ItemId = intNo.ToString();
                il.ImagePath = ci.ImagePath;
                il.ProductName = ci.ProductName;
                il.Price = ci.Price;
                il.Quantity = ci.Quantity;
                il.Spec = ci.Spec;
                il.Mileage = ci.Mileage;
                li.Add(il);
                intNo++;
            }
            //加購
            foreach (BuyPlusItem bpi in liBuyPlus)
            {
                foreach (var jBuyPlus in (JArray)jObj["pBuyPlus_Ary_List"])
                {
                    if (bpi.SMAS_CODE == ((dynamic)jBuyPlus["pSmas_Code_Ary_List"])[0].Smas_Code.ToString())
                    {
                        ItemList il = new ItemList();
                        il.ItemId = intNo.ToString();
                        il.ImagePath = jBuyPlus["IMAGE_PATH_M"].ToString();
                        il.ProductName = jBuyPlus["B2C_CODE_NAME"].ToString();
                        il.Price = Convert.ToInt32(jBuyPlus["Price"]);
                        il.Quantity = 1;
                        il.Spec = ((dynamic)jBuyPlus["pSmas_Code_Ary_List"])[0].Prd_Type.ToString();
                        il.Mileage = "";
                        li.Add(il);
                        intNo++;
                        break;
                    }
                }
            }

            //加贈(隨機與選擇)
            foreach (FreePlusList fpl in liFreePlus)
            {

                ItemList il = new ItemList();
                il.ItemId = intNo.ToString();
                il.ImagePath = fpl.ImagePath;
                il.ProductName = fpl.ProductName;
                il.Price = 0;
                il.Quantity = 1;
                il.Spec = fpl.Prd_Type;
                il.Mileage = "";
                li.Add(il);
                intNo++;
                break;
            }
            gvShoppingList.DataSource = li;
            gvShoppingList.DataBind();


            var ja = jObj["pOrderSumList_Ary"];

            //單價總計(含稅) 
            ltlPriceSum.Text = string.Format("{0:$ #,##0元}", Convert.ToInt32(ja["TotalPriceSum"].ToString()));
            //折扣金額(含稅)=discountSum - eCouponSum
            ltlDiscountSum.Text = string.Format("{0:$ #,##0元}", Convert.ToInt32(ja["discountSum"].ToString()));
            //E-coupon金額
            if (ja["eCouponSum"].ToString() != "null" & !string.IsNullOrEmpty(ja["eCouponSum"].ToString()))
            {
                ltlECouponSum.Text = string.Format("{0:$ #,##0元}", Convert.ToInt32(ja["eCouponSum"].ToString()));
            }
            else
            {
                ltlECouponSum.Text = "$ 0元";
            }
            //運費
            ltlTransFee.Text = string.Format("{0:$ #,##0元}", Convert.ToInt32(ja["TransFee"].ToString()));
            //扣抵總哩程數
            ltlMileSum.Text = string.Format("{0:#,##0哩}", Convert.ToInt32(ja["mile_Sum"].ToString()));
            //消費總計(含稅)
            ltlnTheMoneyWeGet.Text = string.Format("{0:$ #,##0元}", Convert.ToInt32(ja["nTheMoneyWeGet"].ToString()));
            ltlMileSum2.Text = ltlMileSum.Text;
            ltlTotal.Text = ltlnTheMoneyWeGet.Text;
        }
    }

    /// <summary>
    /// 修改
    /// </summary>
    protected void gvShoppingList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        ShoppingCart userShoppingCart = (ShoppingCart)Session["ShoppingCart"];
        if (userShoppingCart != null)
        {
            GridViewRow gvr = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
            int intQty = Convert.ToInt32(((TextBox)gvr.FindControl("txtboxQty")).Text);
            userShoppingCart.UpdateItem(e.CommandArgument.ToString(), intQty);
        }
        BindShoppingList();
    }

    /// <summary>
    /// 下一步
    /// </summary>
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        //進行入驗證
        bool isOK = true;
        Validations val = new Validations();

        //購買人姓名
        if (txtBuyerName.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtBuyerName, ltlBuyerName, "請輸入姓名");
        }
        else
        {
            ControlVerifySetting(true, txtBuyerName, ltlBuyerName);
        }

        //購買人身分證號碼
        if (txtBuyerID.Text.Trim().Length > 0 & !Validations.IsTWID(txtBuyerID.Text.Trim()))
        {
          isOK = false;
          ControlVerifySetting(false, txtBuyerID, ltlBuyerID, "身分證號碼格式錯誤");
        }

        //購買人電話
        if (txtBuyerPhone.Text.Trim().Length == 0)
        {
          isOK = false;
          ControlVerifySetting(false, txtBuyerPhone, ltlBuyerPhone, "請輸入電話");
        }
        else if (txtBuyerPhone.Text.Trim().Length > 0 & !Validations.IsTelephone(txtBuyerPhone.Text.Trim()))
        {
          isOK = false;
          ControlVerifySetting(false, txtBuyerPhone, ltlBuyerPhone, "電話格式錯誤(必須為7碼以上數字)");
        }
        else
        {
            ControlVerifySetting(true, txtBuyerPhone, ltlBuyerPhone);
        }

        //購買人公司電話
        if (txtBuyerTel.Text.Trim().Length > 0 & !Validations.IsTelephone(txtBuyerTel.Text.Trim()))
        {
          isOK = false;
          ControlVerifySetting(false, txtBuyerTel, ltlBuyerTel, "電話格式錯誤(必須為7碼以上數字)");
        }
        else
        {
          ControlVerifySetting(true, txtBuyerTel, ltlBuyerTel);
        }

        //購買人傳真電話
        if (txtBuyerFax.Text.Trim().Length > 0 & !Validations.IsTelephone(txtBuyerFax.Text.Trim()))
        {
          isOK = false;
          ControlVerifySetting(false, txtBuyerFax, ltlBuyerFax, "電話格式錯誤(必須為7碼以上數字)");
        }
        else
        {
          ControlVerifySetting(true, txtBuyerFax, ltlBuyerFax);
        }

        //購買人行動電話
        if (txtBuyerMobile.Text.Trim().Length > 0 & !Validations.IsNumeric(txtBuyerMobile.Text.Trim()))
        {
          isOK = false;
          ControlVerifySetting(false, txtBuyerMobile, ltlBuyerMobile, "行動電話格式錯誤(必須為數字)");
        }
        else
        {
          ControlVerifySetting(true, txtBuyerMobile, ltlBuyerMobile);
        }

        //購買人E-mail
        if (txtBuyerEamil.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtBuyerEamil, ltlBuyerEamil, "請輸入E-mail");
        }
        else if (txtBuyerEamil.Text.Trim().Length > 0 & !Validations.IsEmail(txtBuyerEamil.Text.Trim()))
        {
            isOK = false;
            ControlVerifySetting(false, txtBuyerEamil, ltlBuyerEamil, "E-mail格式錯誤");
        }
        else
        {
            ControlVerifySetting(true, txtBuyerEamil, ltlBuyerEamil);
        }



        //購買人Zip
        if (txtBuyerZIP.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtBuyerZIP, ltlBuyerZIP, "請選擇縣市鄉鎮", "s");
        }
        else
        {
            ControlVerifySetting(true, txtBuyerZIP, ltlBuyerZIP, "", "s");
        }

        //購買人地址
        if (txtBuyerAddress.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtBuyerAddress, ltlBuyerAddress, "請輸入地址", "l");
        }
        else
        {
            ControlVerifySetting(true, txtBuyerAddress, ltlBuyerAddress, "", "l");
        }

        //信用卡號相關驗證
        if (oi.PayType == "1")
        {
            //信用卡號
            if (txtCreditNumber1.Text.Trim().Length == 0 || txtCreditNumber1.Text.Trim().Length != 4)
            {
                isOK = false;
                ControlVerifySetting(false, txtCreditNumber1, ltlCreditNumber1, "請輸入", "s first");
            }
            else
            {
                ControlVerifySetting(true, txtCreditNumber1, ltlCreditNumber1, "", "s first");
            }
            if (txtCreditNumber2.Text.Trim().Length == 0 || txtCreditNumber2.Text.Trim().Length != 4)
            {
                isOK = false;
                ControlVerifySetting(false, txtCreditNumber2, ltlCreditNumber2, "請輸入", "s");
            }
            else
            {
                ControlVerifySetting(true, txtCreditNumber2, ltlCreditNumber2, "", "s");
            }
            if (txtCreditNumber3.Text.Trim().Length == 0 || txtCreditNumber3.Text.Trim().Length != 4)
            {
                isOK = false;
                ControlVerifySetting(false, txtCreditNumber3, ltlCreditNumber3, "請輸入", "s");
            }
            else
            {
                ControlVerifySetting(true, txtCreditNumber3, ltlCreditNumber3, "", "s");
            }
            if (txtCreditNumber4.Text.Trim().Length == 0
              || (ddlCreditType.SelectedValue == "VI" & txtCreditNumber4.Text.Trim().Length != 4) 
              || (ddlCreditType.SelectedValue == "MT" & txtCreditNumber4.Text.Trim().Length != 4)
              || (ddlCreditType.SelectedValue == "JC" & txtCreditNumber4.Text.Trim().Length != 4) 
              || (ddlCreditType.SelectedValue == "AX" & txtCreditNumber4.Text.Trim().Length != 3) //AX為15碼
              || (ddlCreditType.SelectedValue == "DC" & txtCreditNumber4.Text.Trim().Length != 2)) //DC為14碼
            {
                isOK = false;
                ControlVerifySetting(false, txtCreditNumber4, ltlCreditNumber4, "請輸入", "s");
            }
            else
            {
                ControlVerifySetting(true, txtCreditNumber4, ltlCreditNumber4, "", "s");
            }

            //驗證碼，AE可能為3或4碼
            if (txtAuthCode.Text.Trim().Length == 0 || txtAuthCode.Text.Trim().Length < 3)
            {
                isOK = false;
                ControlVerifySetting(false, txtAuthCode, ltlAuthCode, "請輸入", "sm security");
            }
            else
            {
                ControlVerifySetting(true, txtAuthCode, ltlAuthCode, "", "sm security");
            }

           
            //WS驗證
            //string strCreditNumber = txtCreditNumber1.Text.Trim() + txtCreditNumber2.Text.Trim()
            //                           + txtCreditNumber3.Text.Trim() + txtCreditNumber4.Text.Trim();
            //System.Text.StringBuilder sbCreditCard = new System.Text.StringBuilder("{");
            //sbCreditCard.Append("\"creditType\":\"" + ddlCreditType.SelectedValue + "\"");
            //sbCreditCard.Append(",\"creditNumber\":\"" + strCreditNumber + "\"");
            //sbCreditCard.Append(",\"creditExpire\":\"" +  ddlCreditExpireYear.SelectedValue + ddlCreditExpireMonth.SelectedValue + "\"");
            //sbCreditCard.Append(",\"AuthCode\":\"" + txtAuthCode.Text.Trim() + "\"}");
            // string strJson =new B2C_MALL_ORDER().B2C_MALL_check_credit_input(CommonUtil.GetWebDevice(),"",sbCreditCard.ToString());
            //JObject jObj = JsonConvert.DeserializeObject<JObject>(strJson);
            // if (jObj["Is_Valid"].ToString() != "Y")
            // {
            //    isOK = false;
            //    ControlVerifySetting(false, txtCreditNumber1, ltlCreditNumber1, "格式錯誤", "sm security");
            // }
            // else
            // {
            //    ControlVerifySetting(true, txtCreditNumber1, ltlCreditNumber1, "", "s first");
            // }

        }

        // 愛心碼
        if (rbReceiptType3.Checked & txtCharityCode.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtCharityCode, ltlCharityCode, "請輸入", "m");
        }
        else
        {
            ControlVerifySetting(true, txtCharityCode, ltlCharityCode, "", "m");
        }

        // 統一編號
        if (rbReceiptType2.Checked & (txtBuyerCompanyID.Text.Trim().Length == 0 || txtBuyerCompanyID.Text.Trim().Length != 8))
        {
            isOK = false;
            ControlVerifySetting(false, txtBuyerCompanyID, ltlBuyerCompanyID, "請輸入統一編號", "m");
        }
        else
        {
            ControlVerifySetting(true, txtBuyerCompanyID, ltlBuyerCompanyID, "", "m");
        }
        // 公司抬頭
        if (rbReceiptType2.Checked & txtBuyerCompanyTitle.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtBuyerCompanyTitle, ltlBuyerCompanyTitle, "請輸入公司抬頭", "m");
        }
        else
        {
            ControlVerifySetting(true, txtBuyerCompanyTitle, ltlBuyerCompanyTitle, "", "m");
        }

        // 手機條碼
        if (rbReceipt2.Checked && txtCellPhoneCode.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtCellPhoneCode, ltlCellPhoneCode, "請輸入手機條碼", "m");
        }
        else if (rbReceipt2.Checked
          && txtCellPhoneCode.Text.Trim().Length > 0
          && !Validations.IsMobileCode39(txtCellPhoneCode.Text.Trim()))
        {
            isOK = false;
            ControlVerifySetting(false, txtCellPhoneCode, ltlCellPhoneCode, "手機條碼驗證失敗", "m");
        }
        else
        {
            ControlVerifySetting(true, txtCellPhoneCode, ltlCellPhoneCode, "", "m");
        }
        if (rbReceipt2.Checked & txtCellPhoneCodeCheck.Text.Trim().Length == 0)
        {
          isOK = false;
          ControlVerifySetting(false, txtCellPhoneCodeCheck, ltlCellPhoneCodeCheck, "請輸入確認手機條碼", "m");
        }
        else if (rbReceipt2.Checked & txtCellPhoneCode.Text != txtCellPhoneCodeCheck.Text)
        {
          isOK = false;
          ControlVerifySetting(false, txtCellPhoneCodeCheck, ltlCellPhoneCodeCheck, "手機條碼與確認手機條碼必須相同", "m");
        }
        else
        {
          ControlVerifySetting(true, txtCellPhoneCodeCheck, ltlCellPhoneCodeCheck, "", "m");
        }

        // 自然人憑證
        if (rbReceipt3.Checked && txtCitiDigiCertificate.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtCitiDigiCertificate, ltlCitiDigiCertificate, "請輸入自然人憑證", "m");
        }
        else if (rbReceipt3.Checked
          && txtCitiDigiCertificate.Text.Trim().Length > 0
          && !Validations.IsPINCode(txtCitiDigiCertificate.Text.Trim()))
        {
            isOK = false;
            ControlVerifySetting(false, txtCitiDigiCertificate, ltlCitiDigiCertificate, "自然人憑證格式錯誤", "m");
        }
        else
        {
            ControlVerifySetting(true, txtCitiDigiCertificate, ltlCitiDigiCertificate, "", "m");
        }

        if (rbReceipt3.Checked & txtCitiDigiCertificateCheck.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtCitiDigiCertificateCheck, ltlCitiDigiCertificateCheck, "請輸入確認自然人憑證", "m");
        }
        else if (rbReceipt3.Checked & txtCitiDigiCertificate.Text != txtCitiDigiCertificateCheck.Text)
        {
            isOK = false;
            ControlVerifySetting(false, txtCitiDigiCertificateCheck, ltlCitiDigiCertificateCheck, "自然人憑證與確認自然人憑證必須相同", "m");
        }
        else
        {
            ControlVerifySetting(true, txtCitiDigiCertificateCheck, ltlCitiDigiCertificateCheck, "", "m");
        }



        //收件人姓名
        if (txtReceiverName.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtReceiverName, ltlReceiverName, "請輸入姓名");
        }
        else
        {
            ControlVerifySetting(true, txtReceiverName, ltlReceiverName);
        }

        //收件人電話
        if (txtReceiverPhone.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtReceiverPhone, ltlReceiverPhone, "請輸入電話");
        }
        else if (txtReceiverPhone.Text.Trim().Length > 0 & !Validations.IsTelephone(txtReceiverPhone.Text.Trim()))
        {
          isOK = false;
          ControlVerifySetting(false, txtReceiverPhone, ltlReceiverPhone, "電話格式錯誤(必須為7碼以上數字)");
        }
        else
        {
            ControlVerifySetting(true, txtReceiverPhone, ltlReceiverPhone);
        }

        //收件人公司電話
        if (txtReceiverTel.Text.Trim().Length > 0 & !Validations.IsTelephone(txtReceiverTel.Text.Trim()))
        {
          isOK = false;
          ControlVerifySetting(false, txtReceiverTel, ltlReceiverTel, "電話格式錯誤(必須為7碼以上數字)");
        }
        else
        {
          ControlVerifySetting(true, txtReceiverTel, ltlReceiverTel);
        }

        //收件人行動電話
        if (txtReceiverMobile.Text.Trim().Length > 0 & !Validations.IsNumeric(txtReceiverMobile.Text.Trim()))
        {
          isOK = false;
          ControlVerifySetting(false, txtReceiverMobile, ltlReceiverMobile, "行動電話格式錯誤(必須為數字)");
        }
        else
        {
          ControlVerifySetting(true, txtReceiverMobile, ltlReceiverMobile);
        }

        //收件人Zip
        if (txtReceiverZIP.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtReceiverZIP, ltlReceiverZIP, "請選擇縣市鄉鎮", "s");
        }
        else
        {
            ControlVerifySetting(true, txtReceiverZIP, ltlReceiverZIP, "", "s");
        }

        //收件人地址
        if (txtReceiverAddress.Text.Trim().Length == 0)
        {
            isOK = false;
            ControlVerifySetting(false, txtReceiverAddress, ltlReceiverAddress, "請輸入地址", "l");
        }
        else
        {
            ControlVerifySetting(true, txtReceiverAddress, ltlReceiverAddress, "", "l");
        }
        //同意電子商務網站個資蒐集
        if (!cbAgree.Checked)
        {
            isOK = false;
            ControlVerifySetting(false, cbAgree, ltlAgree, "請勾選我已詳細閱讀並同意電子商務網站個資蒐集前告知 ", "");
        }
        else
        {
            ControlVerifySetting(true, cbAgree, ltlAgree, "", "");
        }

        if (isOK)
        {
            oi.BuyerName = txtBuyerName.Text.Trim();
            oi.BuyerID = txtBuyerID.Text.Trim().ToUpper();
            oi.BuyerSex = ddlBuyerSex.SelectedValue;
            oi.BuyerBirthday = ddlYear.SelectedValue + ddlMonth.SelectedValue + hiddenDay.Value;
            oi.BuyerPhone = txtBuyerPhone.Text.Trim();
            oi.BuyerFax = txtBuyerFax.Text.Trim();
            oi.BuyerTel = txtBuyerTel.Text.Trim();
            oi.BuyerMobile = txtBuyerMobile.Text.Trim();
            oi.BuyerEamil = txtBuyerEamil.Text.Trim();
            oi.EDM = ddlEDM.SelectedValue;
            oi.BuyerZIP = txtBuyerZIP.Text.Trim();
            oi.BuyerCity = hfBuyerZone1.Value;
            oi.BuyerArea = hfBuyerZone2.Value;
            oi.BuyerAddress = txtBuyerAddress.Text.Trim();
            oi.CopyBuyer = cbCopyBuyer.Checked ? "Y" : "N";
            oi.ReceiverName = txtReceiverName.Text.Trim();
            oi.ReceiverSex = ddlReceiverSex.SelectedValue;
            oi.ReceiverPhone = txtReceiverPhone.Text.Trim();
            oi.ReceiverTel = txtReceiverTel.Text.Trim();
            oi.ReceiverPhone = txtReceiverPhone.Text.Trim();
            oi.ReceiverZIP = txtReceiverZIP.Text.Trim();
            oi.ReceiverCity = hfReceiverZone1.Value;
            oi.ReceiverArea = hfReceiverZone2.Value;
            oi.ReceiverAddress = txtReceiverAddress.Text.Trim();
            oi.IfDonate = rbReceiptType3.Checked ? "Y" : "N";
            oi.CharityCode = txtCharityCode.Text.Trim();
            if (rbReceiptType1.Checked)
            {
                oi.ReceiptType = "個人";
                //電子發票證明聯
                oi.InvoicePrint = rbReceipt1.Checked ? "Y" : "N";
            }
            else if (rbReceiptType2.Checked)
            {
                oi.ReceiptType = "公司";
                //電子發票證明聯
                oi.InvoicePrint = cbReceiptPrint.Checked ? "Y" : "N";
            }
            else
            {
                oi.ReceiptType = "捐贈";
                //電子發票證明聯
                oi.InvoicePrint = "N";
            }
            oi.InvoicePrintDetail = cbReceiptPrintDetail.Checked ? "Y" : "N";


            oi.BuyerCompanyID = txtBuyerCompanyID.Text.Trim();
            oi.BuyerCompanyTitle = txtBuyerCompanyTitle.Text.Trim();
            //手機條碼
            if (rbReceipt2.Checked)
            {
                oi.CellPhoneCode = txtCellPhoneCode.Text.Trim();
            }
            //自然人憑證
            if (rbReceipt3.Checked)
            {
                oi.CitiDigiCertificate = txtCitiDigiCertificate.Text.Trim();
            }
            //付款方式為信用卡
            if (oi.PayType == "1")
            {
                oi.CreditType = ddlCreditType.SelectedValue;
                oi.CreditNumber = txtCreditNumber1.Text.Trim() + txtCreditNumber2.Text.Trim()
                                            + txtCreditNumber3.Text.Trim() + txtCreditNumber4.Text.Trim();
                oi.CreditExpire = ddlCreditExpireYear.SelectedValue + ddlCreditExpireMonth.SelectedValue;
                oi.AuthCode = txtAuthCode.Text.Trim();
            }
            else
            {
                oi.CreditType = string.Empty;
                oi.CreditNumber = string.Empty;
                oi.CreditExpire = string.Empty;
                oi.AuthCode = string.Empty;
            }
            oi.Agree = "Y";
            //紀錄OrderInfo
            Session["OrderInfo"] = oi;
            Response.Redirect("OrderComplete.aspx");
        }

    }


    /// <summary>
    /// 驗證訊息顯示
    /// </summary>
    private void ControlVerifySetting(bool isChecK, WebControl wc, Literal ltl, string errorMsg = "", string orgClass = "")
    {
        if (!isChecK)
        {
            wc.CssClass = orgClass + " error";
            ltl.Text = "<div class=\"error-message\" Style=\"font-size:11px\">" + errorMsg + "</div>";
        }
        else
        {
            wc.CssClass = orgClass;
            ltl.Text = string.Empty;
        }
    }
}